<?php

namespace app\common\model\menu;

use app\common\model\BaseModel;
use think\facade\Cache;

/**
 * 栏目模型
 */
class Menu extends BaseModel
{
    protected $name = 'menu';
    protected $pk = 'menu_id';

    /**
     * 分类详情
     */
    public static function detail($menu_id)
    {
        return static::find($menu_id);
    }

    /**
     * 所有栏目
     */
    public static function getALL()
    {
        $model = new static;
        if (!Cache::get('menu_' . $model::$app_id)) {
            $data = $model->with('mould')
                ->where('is_delete', '=', 0)
                ->order(['sort' => 'asc', 'create_time' => 'asc'])
                ->select();
            $all = !empty($data) ? $data->toArray() : [];
            $tree = [];
            foreach ($all as $first) {
                if ($first['parent_id'] != 0) continue;
                $twoTree = [];
                foreach ($all as $two) {
                    if ($two['parent_id'] != $first['menu_id']) continue;
                    $threeTree = [];
                    foreach ($all as $three)
                        $three['parent_id'] == $two['menu_id']
                        && $threeTree[] = $three;
                    !empty($threeTree) && $two['child'] = $threeTree;
                    array_push($twoTree, $two);
                }
                if (!empty($twoTree)) {
                    $temp_two_tree = array_column($twoTree, 'sort');
                    array_multisort($temp_two_tree, SORT_ASC, $twoTree);
                    $first['child'] = $twoTree;
                }
                array_push($tree, $first);
            }
            Cache::tag('cache')->set('menu_' . $model::$app_id, compact('all', 'tree'));
        }
        return Cache::get('menu_' . $model::$app_id);
    }

    /**
     * 关联图片
     */
    public function mould()
    {
        return $this->belongsTo('app\common\model\mould\Mould', 'mould_id', 'mould_id');
    }

    /**
     * 获取所有栏目(树状结构)
     */
    public static function getCacheTree()
    {
        return self::getALL()['tree'];
    }

    /**
     * 获取所有栏目
     */
    public static function getCacheAll()
    {
        return self::getALL()['all'];
    }

    /**
     * 所有栏目
     */
    public function getALLList($menu_id = 0)
    {
        $model = $this;
        $data = $model->with('mould')
            ->where('is_delete', '=', 0)
            ->order(['sort' => 'asc', 'create_time' => 'asc'])
            ->select();
        $arr = $this->GetTreeList($data);
        if ($menu_id > 0) {
            $detail = $model->where('menu_id', '=', $menu_id)->find();
            foreach ($arr as $key=> &$item) {
                $item['disabled'] = false;
                if ($item['mould_id'] != $detail['mould_id']) {
                    $item['disabled'] = true;
                }
                if ($item['mould_id'] != $detail['mould_id'] && $item['menu_id'] != $detail['parent_id']) {
                    unset($arr[$key]);
                }
            }
        }
        return $arr;
    }

    public function GetTreeList($arr, $pid = 0, $level = 0)
    {
        global $tree;
        foreach ($arr as $key => $val) {
            if ($val['parent_id'] == $pid) {
                $flg = str_repeat('├', $level);
                $val['name'] = $flg . $val['name'];
                $tree[] = $val;
                $this->GetTreeList($arr, $val['menu_id'], $level + 1);
            }
        }
        return $tree;
    }

    public function getTree($arr, $pid = 0, $level = 0)
    {
        $list = array();
        foreach ($arr as $k => $v) {
            if ($v['parent_id'] == $pid) {
                $v['level'] = $level;
                $child = $this->getTree($arr, $v['menu_id'], $level + 1);
                if (count($child)) {
                    $v['leaf'] = false; //是否子节点
                    $v['child'] = $child;
                } else {
                    $v['leaf'] = true;
                }
                $list[] = $v;
            }
        }
        return $list;
    }

}